<template>
    <div>
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
            <el-form-item label="书号" prop="number">
                <el-input v-model="ruleForm.number"></el-input>
            </el-form-item>
            <el-form-item label="书籍名称" prop="name">
                <el-input v-model="ruleForm.name"></el-input>
            </el-form-item>
            <el-form-item label="书籍种类" prop="kind_id">
                <el-select v-model="ruleForm.kind_id" placeholder="请选择性别">
                    <el-option
                    v-for="item in options"
                    :key="item.id"
                    :label="item.type"
                    :value="item.id">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="出版社" prop="pub">
                <el-input v-model="ruleForm.pub" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="价格" prop="price">
                <el-input v-model="ruleForm.price" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="作者" prop="author">
                <el-input v-model="ruleForm.author" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="出版日期" required>
                <el-col :span="11">
                <el-form-item prop="pubdate">
                    <el-date-picker value-format="yyyy-MM-dd" type="date" placeholder="选择日期" v-model="ruleForm.pubdate" style="width: 100%;"></el-date-picker>
                </el-form-item>
                </el-col>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
              <el-button type="danger" @click="resetForm('ruleForm')">取消</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
import request from '@/utils/request';
export default {
    name:'AddBookList',
    props:['options'],
    data() {
      return {
        ruleForm: {
          number:'',
          name: '',
          kind_id: '',
          pub: '',
          price: '',
          author:'',
          pubdate: ''
        },
        rules: {
          number: [
            { required: true, message: '请输入书号', trigger: 'blur' },
          ],
          name: [
            { required: true, message: '请输入书籍名称', trigger: 'blur' },
          ],
          kind_id: [
            { required: true,message: '请选择书籍类型', trigger: 'blur' }
          ],
          pub: [
            { required: true, message: '请输入出版社', trigger: 'change' }
          ],
          price: [
            { required: true, message: '请输入书籍价格', trigger: 'change' }
          ],
          author: [
            { required: true, message: '请输入书籍作者', trigger: 'change' }
          ],
          pubdate: [
            { type: 'string', required: true, message: '请选择出版日期', trigger: 'change' }
          ]
        }
      };
    },
    methods: {
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            // 发送请求
            this.save()
            // 发送请求
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
        this.$emit('onClose')
      },
      save(){
        request.post('/api/page/workbench/book/add.do',{
            number:this.ruleForm.number,
            name:this.ruleForm.name,
            kind_id:this.ruleForm.kind_id,
            pub:this.ruleForm.pub,
            price:this.ruleForm.price,
            author:this.ruleForm.author,
            pubdate:this.ruleForm.pubdate
        }).then(res=>{
            if(res.code==="1"){
                this.$notify.success('新增成功')
                this.$emit('load')
                this.$emit('onConfirm')
            }else{
                this.$notify.error(res.message)
            }
        })
      }
    }
}
</script>

<style>

</style>